package com.platform.web.controller.system;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.enums.BusinessType;
import com.platform.system.domain.ProCollect;
import com.platform.system.service.IProCollectService;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;

/**
 * 收藏Controller
 *
 * @author platform
 * @date 2025-02-23
 */
@Tag(name = "收藏", description = "")
@RestController
@RequestMapping("/system/collect")
public class ProCollectController extends BaseController {
    @Autowired
    private IProCollectService proCollectService;

    /**
     * 查询收藏列表
     */
    @Operation(summary = "查询收藏列表")
    @PreAuthorize("@ss.hasPermi('system:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProCollect proCollect) {
        startPage();
        List<ProCollect> list = proCollectService.selectProCollectList(proCollect);
        return getDataTable(list);
    }

    /**
     * 查询收藏列表
     */
    @Operation(summary = "查询收藏列表")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ProCollect proCollect) {
        startPage();
        List<ProCollect> list = proCollectService.selectProCollectListAll(proCollect);
        return getDataTable(list);
    }

    /**
     * 查询是否收藏
     */
    @Operation(summary = "查询收藏列表")
    @GetMapping("/exist/{productId}")
    public AjaxResult exist(@PathVariable("productId") Long productId) {
        ProCollect proCollect = proCollectService.selectProCollectExist(productId, getUserId());
        return success(proCollect != null);
    }

    /**
     * 导出收藏列表
     */
    @Operation(summary = "导出收藏列表")
    @PreAuthorize("@ss.hasPermi('system:collect:export')")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProCollect proCollect) {
        List<ProCollect> list = proCollectService.selectProCollectList(proCollect);
        ExcelUtil<ProCollect> util = new ExcelUtil<ProCollect>(ProCollect.class);
        util.exportExcel(response, list, "收藏数据");
    }

    /**
     * 获取收藏详细信息
     */
    @Operation(summary = "获取收藏详细信息")
    @PreAuthorize("@ss.hasPermi('system:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(proCollectService.selectProCollectById(id));
    }

    /**
     * 新增收藏
     */
    @Operation(summary = "新增收藏")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping("/addCollect")
    public AjaxResult addCollect(@RequestBody ProCollect proCollect) {
        ProCollect proCollectExist = proCollectService.selectProCollectExist(Long.parseLong(proCollect.getProducproId()), getUserId());
        if (proCollectExist != null) {
            Long[] ids = {proCollectExist.getId()};
            proCollectService.deleteProCollectByIds(ids);
            return success();
        }
        proCollect.setUserId(getUserId());
        proCollect.setCreateBy(getUsername());
        return toAjax(proCollectService.insertProCollect(proCollect));
    }

    /**
     * 新增收藏
     */
    @Operation(summary = "新增收藏")
//    @PreAuthorize("@ss.hasPermi('system:collect:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProCollect proCollect) {
        proCollect.setUserId(getUserId());
        proCollect.setCreateBy(getUsername());
        return toAjax(proCollectService.insertProCollect(proCollect));
    }

    /**
     * 修改收藏
     */
    @Operation(summary = "修改收藏")
//    @PreAuthorize("@ss.hasPermi('system:collect:edit')")
    @Log(title = "收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProCollect proCollect) {
        proCollect.setUpdateBy(getUsername());
        return toAjax(proCollectService.updateProCollect(proCollect));
    }

    /**
     * 删除收藏
     */
    @Operation(summary = "删除收藏")
//    @PreAuthorize("@ss.hasPermi('system:collect:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(proCollectService.deleteProCollectByIds(ids));
    }
}
